package com.cheng.student.controller.servlet;

import com.cheng.student.service.StudentServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.*;
import java.util.List;

@WebServlet(name = "ExcelInServlet", value = "/ExcelInServlet")
public class ExcelStudentInServlet
        extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {

        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
        ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
        InputStream inputStream = null;
        try
        {
            List<FileItem> fileItem = servletFileUpload.parseRequest(request);
            inputStream = fileItem.get(0).getInputStream();
        }
        catch (FileUploadException e)
        {
            e.printStackTrace();
        }

        System.out.println(new StudentServiceImpl().getStudentsByExcel(inputStream,200));
    }
}
